<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\models\system;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "sys_dept".
 *
 * @property int $id 部门id
 * @property string $name 部门名称
 * @property int $parent_id 父部门id
 * @property string|null $parent_name 祖级名称名称
 * @property string|null $levels 祖级ID列表
 * @property string|null $leader 负责人
 * @property string|null $phone 联系电话
 * @property string|null $email 联系电话
 * @property string|null $status 部门状态（1正常 0停用）
 * @property int|null $list_sort 显示顺序
 * @property string|null $remark 备注
 * @property int|null $create_time 创建时间
 * @property int|null $update_time 更新时间
 */
class Dept extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName(): string
    {
        return 'sys_dept';
    }

    /**
     * {@inheritdoc}
     */
    public function rules(): array
    {
        return [
            ['name', 'required'],

            [['leader', 'phone', 'email', 'remark'], 'default', 'value' => ''],
            ['status', 'default', 'value' => '1'],
            ['list_sort', 'default', 'value' => 10],
            ['parent_id', 'default', 'value' => 0],

            [['name', 'leader', 'phone', 'email', 'status', 'remark'], 'trim'],

            [['name', 'email'], 'string', 'max' => 64],
            [['leader', 'phone'], 'string', 'max' => 32],
            ['remark', 'string', 'max' => 255],
            ['status', 'in', 'range' => ['0', '1']],
            [['list_sort'], 'integer'],

            [['create_time', 'update_time', 'levels', 'parent_name'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels(): array
    {
        return [
            'id' => '部门ID',
            'name' => '部门名称',
            'parent_id' => '父部门id',
            'parent_name' => '祖级名称列表',
            'levels' => '祖级ID列表',
            'leader' => '负责人',
            'phone' => '联系电话',
            'email' => '邮箱',
            'status' => '部门状态',
            'list_sort' => '显示顺序',
            'remark' => '备注',
            'create_time' => '创建时间',
            'update_time' => '更新时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function afterDelete()
    {
        if ($this->id) {
            RoleDept::deleteAll(['dept_id' => $this->id]);
            AdminDept::deleteAll(['dept_id' => $this->id]);
        }
        parent::afterDelete();
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors(): array
    {
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
                'value' => function () {
                    return time();
                }
            ]
        ];
    }
}
